package com.lcj.crawler;

import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.lcj.model.CompanyRecruitInfo;
import com.lcj.service.CompanyRecruitInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SaveDxrcPipeline implements Pipeline<DxrcIndexPage> {
    @Resource(name="companyRecruitInfoServiceImpl")
    private CompanyRecruitInfoService companyRecruitInfoService;
    @Override
    public void process(DxrcIndexPage dxrcIndexPage) {
        List<DxrcCompanyRecruit> list = dxrcIndexPage.getDxrcCompanyRecruitList();
        for(DxrcCompanyRecruit dxrcCompanyRecruit : list){
            CompanyRecruitInfo companyRecruitInfo = new CompanyRecruitInfo();
            if(dxrcCompanyRecruit.getRecruitDate() == null){
                dxrcCompanyRecruit.setRecruitDate("当天");
            }
            companyRecruitInfo.setSalary(dxrcCompanyRecruit.getSalary());
            companyRecruitInfo.setCompanyName(dxrcCompanyRecruit.getCompanyName());
            companyRecruitInfo.setRecruitName(dxrcCompanyRecruit.getRecruitName());
            companyRecruitInfo.setRecruitDate(dxrcCompanyRecruit.getRecruitDate());
            companyRecruitInfo.setRecruitUrl(dxrcCompanyRecruit.getRecruitUrl());
            companyRecruitInfo.setSource("大湘人才网");

            companyRecruitInfoService.saveDxrc(companyRecruitInfo);
            //进入公司链接页面爬取联系方式等信息
            String recruitUrl = "";
            recruitUrl = dxrcCompanyRecruit.getRecruitUrl();
            HttpGetRequest sub = new HttpGetRequest(recruitUrl);
            DeriveSchedulerContext.into(sub);
        }

        String currentPageStr = dxrcIndexPage.getCurrPage();
        int currentPage = getPageInt(currentPageStr);
        int totalPage = 247;
        if(currentPage<totalPage){
            int nextPage = currentPage+1;
            String nextUrl = "http://zp.hnzpsc.com/Jobs/jobs_list/p/" + nextPage +".html";
            HttpRequest request = dxrcIndexPage.getRequest().subRequest(nextUrl);
            DeriveSchedulerContext.into(request);
        }
    }

    private int getPageInt(String totalPageStr) {
        if(StringUtils.isEmpty(totalPageStr)) {
            return 1;
        }
        return NumberUtils.toInt(totalPageStr, 1);
    }
}
